package sort;

import java.util.Stack;

public class sort {

    public static void insertsort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    /*public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap /= 2;
            shell(array, gap);
        }
    }

    public static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];
                } else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }

    }*/


    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap /= 2;
            shell(array, gap);
        }
    }

    public static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];
                } else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }

    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            Swap(array, minIndex, i);
        }
    }

    public static void Swap(int[] array, int x, int y) {
        int tmp = array[x];
        array[x] = array[y];
        array[y] = tmp;
    }

    public static void heapSort(int[] array) {
        createHeap(array);
        int end = array.length - 1;
        while (end > 0) {
            Swap(array, 0, end);
            siftdown(array, 0, end);
            end--;
            /*siftdown(array, 0, end);
            end--;这两种写法哪个对？？*/
        }

    }

    public static void createHeap(int[] array) {
        for (int parent = (array.length - 1 - 1) / 2; parent >= 0; parent--) {
            siftdown(array, parent, array.length);
        }
    }

    public static void siftdown(int[] array, int parent, int len) {
        int child = parent * 2 + 1;
        while (child < len) {
            if (child + 1 < len && array[child] < array[child + 1]) {
                child++;
            }
            if (array[parent] < array[child]) {
                Swap(array, child, parent);
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }

    /*public static void testQuickSort(int[] array) {
        QuickSort(array, 0, array.length - 1);
    }

    private static void QuickSort(int[] array, int left, int right) {
        if (left > right) {
            return;
        }
        int pat = Sort(array, left, right);
        QuickSort(array, left, right - 1);
        QuickSort(array, left + 1, right);
    }

    private static int Sort1(int[] array, int start, int end) {
        int pivot = array[start];
        int i = start;
        while (start < end) {
            while (start < end && array[start] <= pivot) {
                start++;
            }
            while (start < end && array[end] >= pivot) {
                end--;
            }
            Swap(array, start, end);
        }
        Swap(array, start, i);
        return start;
    }

    *//*private static int swap(int[] array,int left,int right){
        int tmp=array[left];
        array[left]=array[right];
        array[right]=tmp;
        return
    }*//*

    private static int Sort(int[] array, int start, int end) {
        int pivot = array[start];
        int i = start;
        while (start < end) {
            while (start < end && array[end] >= pivot) {
                end--;
            }
            array[start] = array[end];
            while (start < end && array[start] <= pivot) {
                start++;
            }
            array[end] = array[start];
        }
        array[start] = pivot;
        return start;
    }


    public static void quickSort2(int[] array) {
        int left = 0;
        int right = array.length-1;
        int par = Sort(array,left,right);

        Stack<Integer> stack = new Stack<>();
        if(par > left+1) {
            stack.push(left);
            stack.push(par-1);
        }
        if(par < right-1) {
            stack.push(par+1);
            stack.push(right);
        }
        while (!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            par = Sort(array,left,right);
            if(par > left+1) {
                stack.push(left);
                stack.push(par-1);
            }
            if(par < right-1) {
                stack.push(par+1);
                stack.push(right);
            }
        }
    }

*/

    public static void quickSort(int[] array) {

        quick(array, 0, array.length - 1);
    }

    private static void quick(int[] array, int left, int right) {
        if (left >= right) {
            return;
        }
        if (right - left + 1 == 3) {
            insertsort2(array, left, right);
            return;
        }
        int index = midThreeNum(array, left, right);
        Swap(array, left, index);
        int mid = partition(array, left, right);
        quick(array, left, mid - 1);
        quick(array, left + 1, right);
    }

    private static int partition(int[] array, int left, int right) {
        int key = array[left];
        while (left < right) {
            while (left < right && array[right] >= key) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= key) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = key;
        return left;
    }

    private static int midThreeNum(int[] array, int left, int right) {
        int mid = (right + left) / 2;
        if (array[left] < array[right]) {
            if (array[left] > array[left]) {
                return left;
            } else if (array[right] < array[mid]) {
                return right;
            } else {
                return mid;
            }
        } else {
            if (array[left] < array[mid]) {
                return left;
            } else if (array[right] > array[mid]) {
                return right;
            } else {
                return mid;
            }
        }
    }

    public static void insertsort2(int[] array, int left, int right) {
        for (int i = left + 1; i <= right; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= left; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }




    public static void quickSortStack(int[] array) {
        int left = 0;
        int right = array.length - 1;
        int par = partition(array, left, right);

        Stack<Integer> stack = new Stack<>();
        if (par > left + 1) {
            stack.push(left);
            stack.push(par - 1);
        }
        if (par < right - 1) {
            stack.push(par + 1);
            stack.push(right);
        }
        while (!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            par = partition(array, left, right);
            if (par > left + 1) {
                stack.push(left);
                stack.push(par - 1);
            }
            if (par < right - 1) {
                stack.push(par + 1);
                stack.push(right);
            }
        }
    }

    /**
     * 归并排序
     * 时间复杂度：O(n*logN)
     * 空间复杂度：O(n)
     * 稳定性：稳定性
     *      插入   冒泡  归并
     * @param array
     */
    public static void mergeSort1(int[] array) {
        mergeSortFunc(array,0,array.length-1);
    }

    public static void mergeSortFunc(int[] array,int left,int right) {
        if(left == right) {
            return;
        }
        int mid = (left + right) / 2;
        //分解
        mergeSortFunc(array,left,mid);
        mergeSortFunc(array,mid+1,right);

        //合并操作
        merge(array,left,right,mid);
    }

    private static void merge(int[] array, int left, int right, int mid) {
        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;
        int[] tmpArr = new int[right-left+1];
        int k = 0;
        //此时2个数组 都至少有一个数据
        while(s1 <= e1 && s2 <= e2) {
            if(array[s1] <= array[s2]) {
                tmpArr[k++] = array[s1++];
            }else{
                tmpArr[k++] = array[s2++];
            }
        }
        while (s1 <= e1) {
            tmpArr[k++] = array[s1++];
        }
        while (s2 <= e2) {
            tmpArr[k++] = array[s2++];
        }
        //保证tmpArr当中的元素是有序的
        for (int i = 0; i < tmpArr.length; i++) {
            array[i+left] = tmpArr[i];
        }
    }

    public static void mergeSort(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i += 2*gap) {
                int left = i;
                int mid = left+gap-1;
                int right = mid+gap;
                if(mid >= array.length) {
                    mid = array.length-1;
                }
                if(right >= array.length) {
                    right = array.length-1;
                }
                merge(array,left,right,mid);
            }
            gap *= 2;
        }
    }


}


